import { AppCTX } from "../../types/koaExtend";
import { isArray, isUndefined, _pick } from "mmb";
import { createBrandApiMWRouter } from "../../middleware/brand-api/brandAdminChecker";
import pageMW from "../../middleware/pager";
import {
  createBrandWarehouse,
  updateBrandWarehouse,
  getBrandWarehouse,
  listBrandWarehousesByBrandID,
} from "../../service/brandWarehouseService";
import DeliveryMode, { isDeliveryMode } from "../../bg-enums/deliveryMode";

const router = createBrandApiMWRouter();

// 获取品牌仓库列表
router.get(
  "brand-warehouses",
  "/brd/:brandId/warehouses",
  pageMW,
  async (ctx: AppCTX, next) => {
    const { brandId } = ctx.params;
    const { userId, limit, offset } = ctx;

    const { total, items } = await listBrandWarehousesByBrandID(brandId, {
      limit,
      offset,
    });
    ctx.body = {
      total,
      rows: items.map((it) => ({
        ..._pick(
          it,
          "brandWarehouseId",
          "name",
          "address",
          "linkman",
          "tel",
          "deliveryModes"
        ),
      })),
    };
  }
);

// 获取品牌仓库
router.get(
  "brand-warehouse",
  "/brd/:brandId/warehouse/:brandWarehouseId",
  async (ctx: AppCTX, next) => {
    const { brandId, brandWarehouseId } = ctx.params;
    const { userId } = ctx;
    const brandWarehouse = await getBrandWarehouse(brandWarehouseId);
    ctx.body = {
      ..._pick(
        brandWarehouse,
        "brandWarehouseId",
        "name",
        "address",
        "linkman",
        "tel",
        "lng",
        "lat",
        "deliveryModes"
      ),
    };
  }
);

// 新增品牌仓库
router.post("/brd/:brandId/warehouse", async (ctx: AppCTX, next) => {
  const { brandId } = ctx.params;
  const {
    name,
    tel,
    linkman,
    address,
    lng,
    lat,
    deliveryModes,
    remotePK,
  } = ctx.request.body;

  const { userId } = ctx;
  if (!isUndefined(deliveryModes)) {
    if (
      !isArray(deliveryModes) ||
      deliveryModes.length === 0 ||
      deliveryModes.some((mode) => !isDeliveryMode(mode))
    ) {
      ctx.throw("400", "deliveryModes invalid");
    }

    if (deliveryModes.some((mode) => mode === DeliveryMode.IN_CITY)) {
      ctx.throw("400", "deliveryMode in_city not imp");
    }
  } else {
    ctx.throw("400", "deliveryModes empty");
  }

  const brandWarehouse = await createBrandWarehouse({
    brandId,
    name,
    address,
    lat,
    lng,
    linkman,
    tel,
    deliveryModes,
  });
  ctx.body = {
    ..._pick(
      brandWarehouse,
      "brandWarehouseId",
      "name",
      "address",
      "lat",
      "lng",
      "linkman",
      "tel",
      "deliveryModes"
    ),
  };
});

// 移除品牌仓库
router.delete(
  "/brd/:brandId/warehouse/:warehouseId",
  async (ctx: AppCTX, next) => {
    const { brandId, warehouseId } = ctx.params;
    const { userId } = ctx;
    //1 userId(操作人不是管理员),报错
    //2 需要发送同步消息给仓库系统
    ctx.body = {
      userId, //操作人
      warehouseId,
    };
  }
);

// 更新品牌仓库
router.put(
  "/brd/:brandId/warehouse/:brandWarehouseId",
  async (ctx: AppCTX, next) => {
    const { brandId, brandWarehouseId } = ctx.params;
    const {
      name,
      tel,
      linkman,
      address,
      lng,
      lat,
      deliveryModes,
    } = ctx.request.body;
    const { userId } = ctx;
    if (!isUndefined(deliveryModes)) {
      if (
        !isArray(deliveryModes) ||
        deliveryModes.length === 0 ||
        deliveryModes.some((mode) => !isDeliveryMode(mode))
      ) {
        ctx.throw("400", "deliveryModes invalid");
      }

      if (deliveryModes.some((mode) => mode === DeliveryMode.IN_CITY)) {
        ctx.throw("400", "deliveryMode in_city not imp");
      }
    } else {
      ctx.throw("400", "deliveryModes empty");
    }

    const brandWarehouse = await updateBrandWarehouse({
      brandWarehouseId,
      name,
      address,
      lat,
      lng,
      linkman,
      tel,
      deliveryModes,
    });
    ctx.body = {
      ..._pick(
        brandWarehouse,
        "brandWarehouseId",
        "name",
        "address",
        "lat",
        "lng",
        "linkman",
        "tel",
        "deliveryModes"
      ),
    };
  }
);

export default router;
